﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif
using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.Enum;
using Kugar.MVC.MobileUI.WeUI.Helpers;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons
{
    /// <summary>
    /// 消息页面,可以使用 WebUIJS.ShowMsgPageByUrl 弹出,也可以直接在页面中输出
    /// </summary>
    public class PageCommon_MsgPage:ControlBase
    {
        private string _title = "操作成功";
        private string _desc = "";
        private IconStyleTypeEnum _iconClass = IconStyleTypeEnum.Success;
        private List<ButtonItemBase> _lst=new List<ButtonItemBase>();

        public PageCommon_MsgPage(ViewContext viewContext) : base(viewContext)
        {
        }

        /// <summary>
        /// 消息页大标题
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public PageCommon_MsgPage Title(string title)
        {
            _title = title;

            return this;
        }

        /// <summary>
        /// 消息页注释区域文本
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public PageCommon_MsgPage Desc(string text)
        {
            _desc = text;

            return this;
        }

        /// <summary>
        /// 添加一个按钮
        /// </summary>
        /// <param name="configFunc"></param>
        /// <returns></returns>
        public PageCommon_MsgPage AddButton(Action<ButtonItemBase> configFunc)
        {
            var config=new ButtonItemBase();
            config.Class("weui-btn");

            configFunc(config);

            _lst.Add(config);

            return this;
        }

        /// <summary>
        /// 图标类型
        /// </summary>
        /// <param name="style"></param>
        /// <returns></returns>
        public PageCommon_MsgPage IconType(IconStyleTypeEnum style)
        {
            _iconClass = style;

            return this;
        }

        public override MvcHtmlString Render()
        {
            ViewContext.Writer.WriteLine("<div class=\"weui-msg\">");

            ViewContext.Writer.WriteLine($"<div class=\"weui-msg__icon-area\"><i class=\"{styleToString(_iconClass)} weui-icon_msg\"></i></div>");

            ViewContext.Writer.WriteLine("<div class=\"weui-msg__text-area\">");

            if (!string.IsNullOrWhiteSpace(_title))
            {
                ViewContext.Writer.WriteLine($"<h2 class=\"weui-msg__title\">{_title}</h2>");
            }

            if (!string.IsNullOrWhiteSpace(_desc))
            {
                ViewContext.Writer.WriteLine($"<p class=\"weui-msg__desc\">{_desc}</p>");
            }

            ViewContext.Writer.WriteLine("</div> <!-- end weui-msg__text-area -->");

            if (_lst.HasData())
            {
                ViewContext.Writer.WriteLine("<div class=\"weui-msg__opr-area\">");

                ViewContext.Writer.WriteLine("<p class=\"weui-btn-area\">");

                foreach (var config in _lst)
                {
                    config.Class(Utility.ButtonStyleToStr(config.getButtonStyle));

                    if (config.getType== ButtonTypeEnum.Submit)
                    {
                        config.OnClick(config._clickScript.ToStringEx() + ";WebUIJS.Submit(this);");
                    }

                    ViewContext.Writer.WriteLine($"<a {config.Attributes} href=\"{(config.getHref.IsNullOrEmpty()? "javascript:void(0)":config.getHref)}\" {(config.getOnClickScript.IsNotNullOrEmpty()?$"onclick=\"__callBackFuncOrStr('{config.getOnClickScript}',this)\"":"")} id=\"{config.getID}\" >{config.getText}</a>");
                }

                ViewContext.Writer.WriteLine("</p>");

                ViewContext.Writer.WriteLine($"</div> <!-- end opr-area -->");
            }

            ViewContext.Writer.WriteLine("</div> <!-- end weui-msg -->");

            return MvcHtmlString.Empty;
        }

        private string styleToString(IconStyleTypeEnum style)
        {
            switch (style)
            {
                case IconStyleTypeEnum.Success:
                    return "weui-icon-success";
                case IconStyleTypeEnum.Waiting:
                    return "weui-icon-waiting";
                case IconStyleTypeEnum.Warn:
                    return "weui-icon-warn";
                case IconStyleTypeEnum.Info:
                    return "weui-icon-info";
                default:
                    throw new ArgumentOutOfRangeException(nameof(style), style, null);
            }
        }


    }
}